import {Box, HeaderRight, Static, UserList} from '../components'
import {concat, MutationComponent, QueryComponent, warning} from '../util'
import ONE_RESUME_BY_CANDIDATEID from '../graphql/OneResumeByCandidateId.graphql'
import {CurrentUser} from '../graphql'
import Router from 'next/router'
import UPSERT_RESUME from '../graphql/UpsertResume.graphql'
import {primaryColor} from '../styles/common-color'


const CandidateHeader = () => (
    <Box height='4rem' padding='0 3rem' flex='flex' justify='space-between'
         bgColor={primaryColor} shadow='0 1px 6px 0 rgba(44,47,65,0.2)' style={{zIndex: '10'}}
    >
        <Box flex='flex' justify='flex-start'>
            <Static label='蜘蛛云聘' fontSize='1.25rem' color='#fff' marginRight='2rem'/>
            <UserList/>
            <CurrentUser render={({currentUser}) => (
                <Box height='100%' flex='flex' justify='flex-end'>
                    <Static label='招聘会' color='#fff' fontSize='.875rem' marginRight='1rem'
                            onClick={() => Router.push('/candidate/job-fair')}
                            style={{marginLeft:'3rem'}}
                    />
                    <QueryComponent gql={ONE_RESUME_BY_CANDIDATEID}
                                    options={{
                                        variables: {input: {candidateId: currentUser.candidateId}}
                                    }}
                                    render={ownProps => (
                                        <MutationComponent gql={UPSERT_RESUME}
                                                           render={mutate => (
                                                               <Static label='我的简历' color='#fff' fontSize='.875rem'
                                                                       marginRight='1rem'
                                                                       onClick={() => {
                                                                           if (!ownProps || !ownProps.oneResumeByCandidateId) {
                                                                               //创建简历
                                                                               warning({
                                                                                   title: '无简历提醒',
                                                                                   content: '您还没有简历，是否需要立即创建并完善？',
                                                                                   onOk: () => {
                                                                                       const candidateId = currentUser.candidateId
                                                                                       mutate({
                                                                                           variables: {
                                                                                               input: {
                                                                                                   candidateId,
                                                                                                   name: currentUser.name,
                                                                                                   mobile: currentUser.mobile
                                                                                               }
                                                                                           }
                                                                                       })
                                                                                       Router.push({
                                                                                           pathname: '/candidate/resume',
                                                                                           query: {candidateId}
                                                                                       })
                                                                                   }
                                                                               })
                                                                           } else {
                                                                               Router.push({
                                                                                   pathname: '/candidate/resume',
                                                                                   query: {candidateId: currentUser.candidateId}
                                                                               })
                                                                           }
                                                                       }}
                                                               />
                                                           )}
                                        />
                                    )}
                    />
                </Box>
            )}/>
        </Box>
        <HeaderRight/>
    </Box>
);

export default CandidateHeader
